﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Registration : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindCountry();
        }
    }

    private void bindCountry()
    {
        BLLShipment bll = new BLLShipment();
        ddlCountry.DataSource = bll.getCountries();
        ddlCountry.DataTextField = "Country";
        ddlCountry.DataValueField = "ID";
        ddlCountry.DataBind();
    }

    protected void btnRegistration_Click(object sender, EventArgs e)
    {


        var sRegex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        if (!sRegex.IsMatch(txtEmail.Text))
        {
            lblEmailError.Visible = true;
            return;
        }

        if (txtPassword.Text != txtRePassword.Text)
        {
            lblPasswordError.Visible = true;
            return;
        }

        try
        {
            BLLRegistration bllRegistration = new BLLRegistration();
            if (bllRegistration.CheckEmailExist(txtEmail.Text))
            {
                lblEmailExist.Visible = true;
                //ScriptManager.RegisterStartupScript(this, this.GetType(), "err", "alert('Email Address has been exist, please choose another email');", true);
                return;
            }

            tblCustomer customer = new tblCustomer()
            {
                Name = txtName.Text,
                NRIC = txtNRIC.Text,
                Block = txtBlock.Text,
                Street = txtStreet.Text,
                City = txtCity.Text,
                Province = txtProvince.Text,
                Country = Convert.ToInt16(ddlCountry.SelectedValue),
                Contact = txtContact.Text,
                Email = txtEmail.Text,
                Password = new Hash().hash(txtPassword.Text),
                IsMember = true
            };

           
            bllRegistration.register(customer);
            try
            {
                bllRegistration.sendEmail(customer.Email);
            }
            catch { ;}
            LoginedMember = customer;
            Response.Redirect("~/Index.aspx");
        }
        catch
        {

        }
    }
    protected void txtEmail_TextChanged(object sender, EventArgs e)
    {
        var sRegex = new System.Text.RegularExpressions.Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");

        if (!sRegex.IsMatch(txtEmail.Text))
        {

            lblEmailError.Visible = true;
            txtEmail.Focus();

        }
        else
        {
            lblEmailError.Visible = false;
        }

    }
}